<?php
class Order
{
    public $ordertotal;
	public function construct()
	{
		$this->ordertotal=0;
	}
	
	public function index()
	{ 
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile("model/Order.php");
		$smarty = new Smarty();
		$modOrder = new ModelOrder();
		$request = CommonFunc::getRequest();
                $paginatorHelper = new Adminpagination();
                $perpage = ITEMPERPAGE;

			//$page =(int)$request['param1'];
                       // $limit = 'LIMIT 0, 500 ' ;
                       
                        $totalrowcount_not_processed=$modOrder->gettotalrowcount_not_processed();
                        $curPage_not_processed = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_not_processed = 'LIMIT  '.($curPage_not_processed-1)*$perpage." , " . $perpage;
			$item_arr_not_processed = $modOrder->listOrders_not_processed();
                      //  print_r($item_arr_not_processed);
                        $config_not_processed['base_url'] = "order/index/";
                        $config_not_processed['total_rows'] = $totalrowcount_not_processed;
                        $config_not_processed['per_page'] = $perpage;
                        $config_not_processed['cur_page'] = $curPage_not_processed;
                        $paginatorHelper->initialize($config_not_processed);
                        $paginatorhtml_not_processed=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_not_processed", $paginatorhtml_not_processed);
                         $smarty->assign('item_arr_not_processed', $item_arr_not_processed);

                          $totalrowcount_processing=$modOrder->gettotalrowcount_processing();
                        $curPage_processing = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_processing = 'LIMIT  '.($curPage_processing-1)*$perpage." , " . $perpage;
			$item_arr_processing = $modOrder->listOrders_processing();
                      //  print_r($item_arr_not_processed);
                        $config_processing['base_url'] = "order/index/";
                        $config_processing['total_rows'] = $totalrowcount_processing;
                        $config_processing['per_page'] = $perpage;
                        $config_processing['cur_page'] = $curPage_processing;
                        $paginatorHelper->initialize($config_processing);
                        $paginatorhtml_processing=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_processing", $paginatorhtml_processing);
                         $smarty->assign('item_arr_processing', $item_arr_processing);


                        $totalrowcount_delivered_notpaid=$modOrder->gettotalrowcount_delivered_notpaid();
                        $curPage_delivered_notpaid = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_delivered_notpaid = 'LIMIT  '.($curPage-1)*$perpage." , " . $perpage;
			$item_arr_delivered_notpaid = $modOrder->listOrders_delivered_notpaid();
                        $config_delivered_notpaid['base_url'] = "order/index/";
                        $config_delivered_notpaid['total_rows'] = $totalrowcount_delivered_notpaid;
                        $config_delivered_notpaid['per_page'] = $perpage;
                        $config_delivered_notpaid['cur_page'] = $curPage_delivered_notpaid;
                        $paginatorHelper->initialize($config_delivered_notpaid);
                        $paginatorhtml_delivered_notpaid=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_delivered_notpaid", $paginatorhtml_delivered_notpaid);
                         $smarty->assign('item_arr_delivered_notpaid', $item_arr_delivered_notpaid);


                        $totalrowcount_paid_notdelivered=$modOrder->gettotalrowcount_paid_notdelivered();
                        $curPage_paid_notdelivered = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_paid_notdelivered = 'LIMIT  '.($curPage_paid_notdelivered-1)*$perpage." , " . $perpage;
			$item_arr_paid_notdelivered = $modOrder->listOrders_paid_notdelivered();
                        $config_paid_notdelivered['base_url'] = "order/index/";
                        $config_paid_notdelivered['total_rows'] = $totalrowcount_paid_notdelivered;
                        $config_paid_notdelivered['per_page'] = $perpage;
                        $config_paid_notdelivered['cur_page'] = $curPage_paid_notdelivered;
                        $paginatorHelper->initialize($config_paid_notdelivered);
                        $paginatorhtml_paid_notdelivered=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_paid_notdelivered", $paginatorhtml_paid_notdelivered);
                        $smarty->assign('item_arr_paid_notdelivered', $item_arr_paid_notdelivered);

                        $totalrowcount_completed=$modOrder->gettotalrowcount_completed();
                        $curPage_completed = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_completed = 'LIMIT  '.($curPage-1)*$perpage." , " . $perpage;
			$item_arr_completed = $modOrder->listOrders_completed();
                        $config_completed['base_url'] = "order/index/";
                        $config_completed['total_rows'] = $totalrowcount_completed;
                        $config_completed['per_page'] = $perpage;
                        $config_completed['cur_page'] = $curPage_completed;
                        $paginatorHelper->initialize($config_completed);
                        $paginatorhtml_completed=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_completed", $paginatorhtml_completed);
                       $smarty->assign('item_arr_completed', $item_arr_completed);

                        $totalrowcount_cancelled=$modOrder->gettotalrowcount_cancelled();
                        $curPage_cancelled = ( ($request['param1']!=FALSE)? ( (int)$request['param1']/$perpage )+1:1 );
                        $limit_cancelled = 'LIMIT  '.($curPage-1)*$perpage." , " . $perpage;
			$item_arr_cancelled = $modOrder->listOrders_cancelled();
                        $config_cancelled['base_url'] = "order/index/";
                        $config_cancelled['total_rows'] = $totalrowcount_cancelled;
                        $config_cancelled['per_page'] = $perpage;
                        $config_cancelled['cur_page'] = $curPage_cancelled;
                        $paginatorHelper->initialize($config_cancelled);
                        $paginatorhtml_cancelled=$paginatorHelper->create_links();
                        $smarty->assign("paginatorhtml_cancelled", $paginatorhtml_cancelled);
                        $smarty->assign('item_arr_cancelled', $item_arr_cancelled);


                      //  print_r($item_arr);exit;
			$smarty->assign("title", "Order Management");
			
                        $smarty->assign("cat_arr", $cat_arr);
                        $smarty->assign("subcat_arr", $subcat_arr);
                        $smarty->assign("catid", $catid);
                        $smarty->assign("subcatid", $subcatid); 
			$smarty->display('order/index.tpl');
		
		
	}
	
	public function view()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");	
		CommonFunc::loadFile('model/Order.php');
                $creditbalancehtml="";
                $showdeliver=0;
		$smarty = new Smarty();
		$editorFCKHelper = new EditorFCKHelper();		
		$modOrder = new ModelOrder();
		$request = CommonFunc::getRequest();
		$orderid = $request['param1'];
		$order_arr = $modOrder->getOrderByID($orderid);
                $cart_arr=$modOrder->getCart($order_arr[0]["cart_id"]);
                $carthtml=$this->createcarthtml($cart_arr);
                $paidcheck=($order_arr[0]["ispaid"]==1)?"checked":"";
                $deliveredcheck=($order_arr[0]["isdelivered"]==1)?"checked":"";
                $isprocessingcheck=($order_arr[0]["isprocessing"]==1)?"checked":"";
                if($modOrder->iscredituser($order_arr[0]["customer_id"])){
                    if($order_arr[0]["ispaid"]==0){
                         if($modOrder->creditbalance($order_arr[0]["customer_id"])>$this->ordertotal) {
                            $creditbalancehtml= "<span class='success'><a onclick=loadPage('creditor/viewcreditaccount/".$order_arr[0]["customer_id"]."')  href='javascript: void(0);'><strong> Credit Balance LKR ".number_format($modOrder->creditbalance($order_arr[0]["customer_id"]),2)."</strong></a></span>";
                             $showdeliver=1;
                         }
                         else{
                             if($order_arr[0]["isdelivered"]==1){
                                 $showdeliver=1;
                                  $creditbalancehtml= "<span class='success'><a onclick=loadPage('creditor/viewcreditaccount/".$order_arr[0]["customer_id"]."')  href='javascript: void(0);'><strong> Credit Balance LKR ".number_format($modOrder->creditbalance($order_arr[0]["customer_id"]),2)."</strong></a></span>";
                             }
                             else{
                                 $showdeliver=0;
                                 $creditbalancehtml="<span class='error'>This user does not have enough credit to proceed the order&nbsp;<strong> Credit Balance LKR ".number_format($modOrder->creditbalance($order_arr[0]["customer_id"]),2)."</strong>&nbsp;<a onclick=loadPage('creditor/viewcreditaccount/".$order_arr[0]["customer_id"]."')  href='javascript: void(0);'><strong>Assign Credit</strong></a></span>";
                             }
                             
                         }
                    }
                    else{
                        $showdeliver=1;
                         $creditbalancehtml= "<span class='success'><a onclick=loadPage('creditor/viewcreditaccount/".$order_arr[0]["customer_id"]."')  href='javascript: void(0);'><strong> Credit Balance LKR ".number_format($modOrder->creditbalance($order_arr[0]["customer_id"]),2)."</strong></a></span>";
                    }
                   
                }
                else{
                    if($order_arr[0]["ispaid"]==0){
                         $showdeliver=0;
                         $creditbalancehtml="<span class='error'>Cannot deliver until the payment recieve</span>";
                    }
                    else{
                        $showdeliver=1;
                    }
                    
                }
		$smarty->assign("title", "View Order");
		$smarty->assign('objeditorFCK', $editorFCKHelper);
		$smarty->assign('order_arr', $order_arr);
                $smarty->assign('carthtml', $carthtml);
                $smarty->assign('showdeliver', $showdeliver);
                $smarty->assign('creditbalancehtml', $creditbalancehtml);
                $smarty->assign('ordertotal', $this->ordertotal);
                $smarty->assign('paidcheck', $paidcheck);
                $smarty->assign('deliveredcheck', $deliveredcheck);
                $smarty->assign('isprocessingcheck', $isprocessingcheck);
		$smarty->assign("orderid", $orderid);
		$smarty->display("order/vieworder.tpl");
	}

        public function createcarthtml($cartval)
        {
                 $grandtotal=0;
		 $carthtml=" <div class='new_cart'>";
		 $carthtml.=" <table width='100%' id='cart_table' border='0' cellspacing='0v cellpadding='0'>";
		 $carthtml.=" <tr>";
		 $carthtml.=" <th align='left' class='manuf' scope='col'>Item Code</th>";
		 $carthtml.=" <th align='left' class='desc' scope='col'>product Name</th>";
		 $carthtml.=" <th align='right' class='unit_price' scope='col'>unit price(LKR)</th>";
		 $carthtml.=" <th class='qty' scope='col'>Qty</th>";
                 $carthtml.=" <th align='right' class='total_price' scope='col'>total</th>";
		 $carthtml.=" </tr><tr>";
                 foreach($cartval as $cart){
                  $carthtml.="<tr class='item'>";
	          $carthtml.="<td class='fst'>".$cart['item_code']."</td>";
	          $carthtml.="<td align='center'>".$cart['product_title']."</td>";
		  $carthtml.="<td  align='center'width='16%'>".number_format($cart['price'],2)."</td>";
			    	 $carthtml.="<td align='right'>".$cart['quantity']."</td>";
			    	 $carthtml.="<td align='right'>".number_format($cart['quantity']* $cart['price'],2)."</td>";
			    	 $grandtotal+= (int)$cart['quantity']* (int)$cart['price'];
				 }

			    /*	if($deliverycharge>0){
				    $carthtml.=" </tr><tr>";
				 	$carthtml.=" <td>&nbsp;</td>";
				 	$carthtml.=" <td>&nbsp;</td>";
				 	$carthtml.=" <td>&nbsp;</td>";
				 	$carthtml.=" <td><strong>Delivery Charge LKR</strong></td>";
				 	$carthtml.=" <td>".$deliverycharge."</td>";
				 	$grandtotal+= $deliverycharge;
			    	}
*/
				    $carthtml.=" </tr><tr class='total_amount sumval'> ";
				 	$carthtml.=" <td>&nbsp;</td>";
				 	$carthtml.=" <td>&nbsp;</td>";
				 	$carthtml.=" <td  colspan='2' align='right' class='lbl'> TOTAL LKR </td>";
				 	$carthtml.=" <td colspan='2' align='right' class='amount'>".number_format($grandtotal,2)."</td></tr></tbody></table>";
                              $carthtml.=" </div> ";

                          $this->ordertotal=$grandtotal;
		           return  	$carthtml;


            }

        public function addProduct()
	{
		CommonFunc::loadFile(SMARTYLIBS . "Smarty.class.php");
		CommonFunc::loadFile(ADAPTOR . "/EditorFCK.php");
		CommonFunc::loadFile('model/Category.php');
		CommonFunc::loadFile('model/Product.php');
		$smarty = new Smarty();
		$editorFCKHelper = new EditorFCKHelper();
		$modCategory = new ModelCategory();
		$modPRoduct = new ModelProduct();
		$cat_arr = $modCategory->listCategoriesfordropdown();
		$request = CommonFunc::getRequest();
		$itemcode = $request['param1'];
		$product_arr = $modPRoduct->getProductByItemCode($itemcode);
		$smarty->assign("title", "Add Product");
		$smarty->assign('objeditorFCK', $editorFCKHelper);
		$smarty->assign('product_arr', $product_arr);
		$smarty->assign("cat_arr", $cat_arr);
		$smarty->assign("itemcode", $itemcode);
		$smarty->display("product/addproduct.tpl");
	}


}	
?>
